package com.rdc.qingkou.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;


public class VpSwipeRefreshLayout extends SwipeRefreshLayout {

    private float startY;
    private float startX;
    private boolean mIsVpDragger;
    private int mTouchSlop;//用户滑动的最小值

    public VpSwipeRefreshLayout(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public VpSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //记录手指开始按下的位置
                startX = event.getX();
                startY = event.getY();
                //初始化标记
                mIsVpDragger = false;
                break;
            case MotionEvent.ACTION_MOVE:
                //如果ViewPager正在拖拽中，则不拦截它的事件
                if (mIsVpDragger) {
                    return false;
                }
                //获取当手指的位置
                float endX = event.getX();
                float endY = event.getY();
                float distanceX = Math.abs(endX - startX);
                float distanceY = Math.abs(endY - startY);
                //如果X轴位移大于Y轴位移，那么将事件交给ViewPager处理
                if (distanceX > mTouchSlop && distanceX > distanceY) {
                    mIsVpDragger = true;
                    return false;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mIsVpDragger = false;
                break;
        }
        return super.onInterceptTouchEvent(event);
    }
}
